package com.decades.application.adapter;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.os.StrictMode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.decades.application.dto.BlogDto;
import com.decades.application.json.JSONParserForGetList;
import com.decades.application.main.ContactUsActivity;
import com.decades.application.main.ContentMainActivity;
import com.decades.application.main.InformDetailActivity;
import com.decades.application.main.KMSMenuActivity;
import com.decades.application.main.PublishDocumentPageActivity;
import com.decades.application.main.R;
import com.jakewharton.salvage.ProgressHUD;
import com.squareup.picasso.Picasso;

@SuppressLint("NewApi") public class GridViewImageAdapter extends BaseAdapter {

	private Activity _activity;
	private List<BlogDto> menus;
	private int imageWidth;
	private ViewHolder viewHolder;

	public GridViewImageAdapter(Activity activity, List<BlogDto> menus,int imageWidth) {
		this._activity = activity;
		this.menus = menus;
		this.imageWidth = imageWidth;
	}

	@Override
	public int getCount() {
		return this.menus.size();
	}

	@Override
	public Object getItem(int position) {
		return this.menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		BlogDto menu = menus.get(position);
		if (convertView == null) {
			viewHolder = new ViewHolder(); 
			LayoutInflater inflater = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = inflater.inflate(R.layout.each_box_layout, null);
			viewHolder.relative = (RelativeLayout) convertView.findViewById(R.id.clickLayout);
			viewHolder.titleImage = (TextView) convertView.findViewById(R.id.textView1);
			viewHolder.showCase = (ImageView) convertView.findViewById(R.id.imageView1);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag(); 
		}
		
		Picasso.with(_activity.getApplicationContext()).load(menu.getContent()).placeholder(R.drawable.defaultmenu).into(viewHolder.showCase);			

		convertView.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, (imageWidth+45)));
		viewHolder.relative.setOnClickListener(new OnBlogSelectedListener(position,convertView,menu));
		viewHolder.titleImage.setText(menu.getTitle());
		return convertView;
	}

	public class ViewHolder {
		private ImageView showCase;
		private TextView titleImage;
		private RelativeLayout relative;
	}
	public class OnBlogSelectedListener implements OnClickListener{
		private int _position;
		public OnBlogSelectedListener(int position,View btnView,BlogDto blogDto){
			this._position = position;
		}
		
		@SuppressLint("NewApi")
		@Override
		public void onClick(View v) {
			if (android.os.Build.VERSION.SDK_INT > 9) {
			      StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
			      StrictMode.setThreadPolicy(policy);
			 }	
			new GeInformationDetail(_position).execute();
		}
		
	}
	class OnImageClickListener implements OnClickListener {
		Intent i ;
		String id;
		String _str;
		// constructor
		public OnImageClickListener(String id,String str) {
			this.id = id;
			this._str = str;
		}

		@Override
		public void onClick(View v) {
				if(!"888".equals(id) &&!"15".equals(id) &&!"18".equals(id)){					
					//InformDetailActivity
					i = new Intent(_activity, InformDetailActivity.class);
					i.putExtra("backAnimation", "fade");
					i.putExtra("ActionBarText", _str);
					i.putExtra("menu_id", id);
					_activity.startActivity(i);
					_activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
					_activity.finish();
				}else if("888".equals(id)){
					i = new Intent(_activity,KMSMenuActivity.class);
					i.putExtra("ActionBarTitle", _activity.getResources().getString(R.string.add_kms_menu_text));
					i.putExtra("StateFrom", 1);
					_activity.startActivity(i);  
					_activity.overridePendingTransition (R.anim.open_next, R.anim.close_main);
					_activity.finish(); 
				}else if("18".equals(id)){  
					i = new Intent(_activity,ContactUsActivity.class);
					i.putExtra("ActionBarTitle", _activity.getResources().getString(R.string.contact_menu_text));
					_activity.startActivity(i);  
					_activity.overridePendingTransition (R.anim.open_next, R.anim.close_main);
				}else if("15".equals(id)){
					i = new Intent(_activity, PublishDocumentPageActivity.class);
					i.putExtra("ActionBarText", _str);
					_activity.startActivity(i);
					_activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
					_activity.finish();
				}
			
		}

	}
	public class GeInformationDetail extends AsyncTask<String, Void, ArrayList<String>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		int _position;
		BlogDto blogDto;
		
		public GeInformationDetail(int position){
			this._position = position;
		}
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(_activity,"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected ArrayList<String> doInBackground(String... params) {
			// TODO Auto-generated method stub
			ArrayList<String> list = new ArrayList<String>();
			for(BlogDto dto : menus){
				list.add(dto.getContent());
			}
			blogDto = JSONParserForGetList.getInstance().getBlogById(menus.get(_position).getId());
			return list;
		}
		
		@Override
		protected void onPostExecute(ArrayList<String> result) {
			super.onPostExecute(result);
			if (result != null) {
				Intent i = new Intent(_activity,ContentMainActivity.class);
				i.putExtra("blogDto",(Parcelable) blogDto);
				i.putExtra("blogDto.id",blogDto.getId());
				i.putExtra("blogDto.getAuthor_name",blogDto.getAuthor_name());
				i.putExtra("blogDto.getContent", blogDto.getContent());
				i.putExtra("blogDto.getMindmap", blogDto.getMindmap());
				i.putExtra("blogDto.getTitle", blogDto.getTitle());
				i.putExtra("blogDto.getCreate_date", blogDto.getCreate_date());
				
				i.putStringArrayListExtra("list", result);
				_activity.startActivity(i);	
				mProgressHUD.dismiss();
			} else {
				mProgressHUD.dismiss();
				(Toast.makeText(_activity, "An error occured.", Toast.LENGTH_LONG)).show();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}

	}
}
